<style>
    .layui-form-label.required:before { /* 这种写法也可以*/
        content: ' *';
        color: red;
    }
    .open-form .layui-form-label {
        width: 110px;
    }
    .open-form .layui-input-block{
        margin-left: 140px;
    }
</style>

<!-- bom表格操作列 -->
<script type="text/html" id="bomProductPlanningTableBar">
    {{# if(d.assemblyNature != '3'){ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="addDownInfo">添加下级Bom</a>
    {{#}}}
    <!--    {{# if(d.assemblyNature == '3'){ }}-->
    <!--    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="blueprints">图纸管理</a>-->
    <!--    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="processRoute">工艺路线</a>-->
    <!--    {{#}}}-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="removeInfo">移除</a>
</script>

<script type="text/javascript" src="../../../admin/assets/js/pdfobject.min.js"></script>
<!-- 表单弹窗 -->
<form lay-filter="proTaskZYForm" class="layui-form proTaskZYForm open-form">
    <div class="open-model">
        <div class="open-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>产品信息</legend>
            </fieldset>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label required">来源：</label>
                    <div class="layui-input-block">
                        <input name="source" disabled type="text" class="layui-input"
                               required autocomplete="off" value="自研产品"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">产品类别：</label>
                    <div class="layui-input-block">
                        <input name="productTypeName" disabled type="text" class="layui-input"
                               lay-verify="required"/>
                        <input id="productTypeId" name="productTypeId" type="hidden" value="' + d.proTypeId + '"
                               lay-verify="required">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">产品性质：</label>
                    <div class="layui-input-block">
                        <input name="productNatureName" disabled type="text" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">产品属性：</label>
                    <div class="layui-input-block">
                        <input name="assemblyNatureName" disabled type="text" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>

            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部SKU号：</label>
                    <div class="layui-input-block" id="SkuInfo">
                        <input id="productDetailId" name="productDetailId" type="hidden">
                        <input id="inSkuCode" name="inSkuCode" disabled type="text" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部SKU名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inSkuName" disabled name="inSkuName" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部规格码：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inSkuSpec" disabled name="inSkuSpec" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">基础单位：</label>
                    <div class="layui-input-block">
                        <input name="inUnitName" disabled type="text" class="layui-input"
                               lay-verify="required"/>
                    </div>
                </div>
                <!--        <div class="layui-col-md3">-->
                <!--            <label class="layui-form-label">计量单位：</label>-->
                <!--            <div class="layui-input-block">-->
                <!--                <input name="inSkuUnitName" disabled type="text" class="layui-input"-->
                <!--                       lay-verify="required"/>-->
                <!--            </div>-->
                <!--        </div>-->
            </div>

            <div class="layui-form-item layui-row">

                <!--        <div class="layui-col-md3">-->
                <!--            <label class="layui-form-label">基础单位数量：</label>-->
                <!--            <div class="layui-input-block">-->
                <!--                <input type="text" disabled id="inUnitCount" name="inUnitCount" class="layui-input">-->
                <!--            </div>-->
                <!--        </div>-->
                <div class="layui-col-md3">
                    <label class="layui-form-label required">计量方式(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainCalWayName" readonly="readonly" name="mainCalWayName" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">包装单位(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainUnitName" readonly="readonly" name="mainUnitName" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">换算率(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainUnitRate" readonly="readonly" name="mainUnitRate" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">计量方式(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subCalWayName" readonly="readonly"  name="subCalWayName" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">计量单位(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subUnitName" readonly="readonly" name="subUnitName" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">换算率(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subUnitRate" readonly="readonly" name="subUnitRate" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">成本核算类型：</label>
                    <div class="layui-input-block">
                        <select id="costCheckType" disabled name="costCheckType" lay-filter="costCheckType"
                                class="layui-input borderNone"></select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">是否质检：</label>
                    <div class="layui-input-block">
                        <select id="checkStatus" disabled name="checkStatus" lay-filter="checkStatus"
                                class="layui-input borderNone"></select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">是否料箱管理：</label>
                    <div class="layui-input-block">
                        <select id="boxStatus" disabled name="boxStatus" lay-filter="boxStatus"
                                class="layui-input borderNone"></select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">说明：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inRemark" name="inRemark" class="layui-input">
                    </div>
                </div>
            </div>
            <fieldset class="layui-elem-field layui-field-title">
                <legend>其他信息</legend>
            </fieldset>
            <div class="layui-form-item layui-row">
                <div class="layui-tab" lay-filter="test-handle" lay-allowclose="true">
                    <ul class="layui-tab-title">
                        <li class="layui-this" lay-id="YSBZ">产品验收标准</li>
                        <li lay-id="BOMGUIHUA">MRP物料BOM规划</li>
                        <li lay-id="GCGUIHUA">产品工程规划</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-form-item layui-row">
                                <label class="layui-form-label">版本编号：</label>
                                <input style="border: none;" type="text" value="BCZ380C9.2.1.4.1.1.1-2" disabled
                                       id="infoVersion" name="infoVersion" class="layui-input">
                            </div>
                            <div class="layui-form-item layui-row">
                                <div class="layui-col-md5">
                                    <div id="TuZhiPdf" style="height: 100%;margin-left:10px">

                                    </div>
                                </div>
                                <div class="layui-col-md7">
                                    <div class="layui-form-item layui-row">
                                        <button type="button" id="addTestItem" class="layui-btn" lay-filter="addTestItem"
                                                lay-submit>添加检测项
                                        </button>
                                        <table class="layui-table" id="testItemTable" lay-filter="testItemTable"></table>
                                    </div>
                                    <div class="layui-form-item text-right">
                                        <button class="layui-btn" lay-filter="saveTestItem" type="button" id="saveTestItem">
                                            保存检测项
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item" id="bom_info">
                            <div class="layui-form-item layui-row">
                                <div class="layui-form-item layui-row">
                                    <button type="button" id="addBomSKu" class="layui-btn" lay-filter="addBomSKu" lay-submit>添加子件物料</button>
                                    <table class="layui-table" id="bomTreeTableInfo" lay-filter="bomTreeTableInfo"></table>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item" id="gy">
                            <div class="layui-form-item layui-row">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">选择主材：</label>
                                    <div class="layui-input-block">
                                        <select id="mainSkuId" lay-search="" name="mainSkuId"
                                                lay-filter="mainSkuId"
                                                class="common-select-search">
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item layui-row">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">选择工艺路线：</label>
                                    <div class="layui-input-block">
                                        <select id="processLineTemplate" lay-search="" name="processLineTemplate"
                                                lay-filter="processLineTemplate"
                                                class="common-select-search">
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item layui-row">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">关联模具图纸：</label>
                                    <div class="layui-input-block">
                                        <select id="isRelevance" name="isRelevance"
                                                lay-filter="isRelevance"
                                                class="common-select-search">
                                            <option value="0">不关联</option>
                                            <option value="1">关联模具图纸</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-md9" id="moldUploadInfo">
                                    <div class="layui-col-md4">
                                        <label class="layui-form-label">模具图纸：</label>
                                        <div class="layui-input-block">
                                            <div id="fileUploadMoldBtn" class="layui-btn" style="float: left;margin-left: 3px">
                                                <i class="layui-icon">&#xe681;</i>上传文件
                                            </div>
                                            <br/>
                                            <div id="addMoldHeaderPdf" style="margin-top: -16px"
                                                 class="companyFormPhotos"></div>
                                            <input name="attachmentMoldPdf" id="attachmentMoldPdf" type="hidden"
                                                   class="layui-input"/>
                                        </div>
                                    </div>
                                    <div class="layui-col-md7">
                                        <label class="layui-form-label">模具图纸备注：</label>
                                        <div class="layui-input-block">
                                    <textarea id="moldRemark" name="moldRemark" placeholder="模具图纸备注"
                                              class="layui-textarea"
                                              autocomplete="off"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item layui-row">
                                <button type="button" id="addProcessOne" class="layui-btn" lay-filter="addProcessOne"
                                        lay-submit>添加工序
                                </button>
                                <table class="layui-table" id="productProjectTable" lay-filter="productProjectTable"></table>
                            </div>
                            <div class="layui-form-item text-right">
                                <button class="layui-btn" lay-filter="saveProductProject" type="button" id="saveProjectUp">
                                    保存产品规划
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-normal" id="submitProofread" type="button">提交校对</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
        </div>
    </div>
</form>

<script type="text/html" id="viewAttachmentHtml">
    <div id="example" style="height: 100%;"></div>
</script>

<script type="text/html" id="viewAttachmentHtml2">
    <form lay-filter="imgModifyForm" class="layui-form model-form"
          style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;">
        <div class="open-details-box" style="height: calc(100% - 75px);">
            <div class="layui-row table-bor " style="margin-top: 15px;">
                <div class="layui-form-item layui-row">
                    <label class="layui-form-label required">产品图纸：</label>
                    <div class="layui-input-block">
                        <div id="example1" class="uploadImgStyle"></div>
                        <div id="productPicBtn" class="layui-btn" style="float: left;">
                            <i class="layui-icon">&#xe681;</i>上传新图纸
                        </div>
                        <input name="productPic" type="hidden" class="layui-input" lay-verify="" id="productPic"/>
                    </div>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-md12">
                        <label class="layui-form-label required">工艺说明：</label>
                        <div class="layui-input-block">
                        <textarea id="processDescription" name="processDescription" placeholder="工艺说明" class="layui-textarea"
                                  autocomplete="off"/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="imgModifyFormSubmit" lay-submit type="button">保存</button>
        </div>
    </form>
</script>

<!-- 质检表格操作列 -->
<script type="text/html" id="testItemTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delTestItem">删除</a>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="productProcessTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delProcess">删除</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form','tableTreeDj', 'table', 'laydate', 'admin', 'laytpl', 'upload', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var config = layui.config;
        var upload = layui.upload;
        var table = layui.table;
        var admin = layui.admin;
        var form = layui.form;
        var laydate = layui.laydate;
        var tableTree = layui.tableTreeDj;

        let projectTableData = [];
        //初始化data
        let testItemData = [];
        let moldName = "";
        let customerName = "";
        let fileName = "";
        let tzId;
        let tzVersion;
        let data = admin.getTempData('resProTaskZYData');
        let bom_tab = document.querySelector('.layui-tab-title li:nth-child(2)');
        let bom_content = document.querySelector('.layui-tab-content .layui-tab-item:nth-child(2)');
        let gx_tab = document.querySelector('.layui-tab-title li:nth-child(3)');
        let gx_content = document.querySelector('.layui-tab-content .layui-tab-item:nth-child(3)');

        laydate.render({
            elem: '#planGivenTime'
            , type: 'datetime'
        });

        /**
         * 初始化任务详情
         */
        admin.req('enterprise/res/admin/planning/proTask/detail/' + data.id, {}, function (res) {
            if (res.code == 200) {
                form.val('proTaskZYForm', res.data === undefined ? null : res.data);

                admin.getDictList("costCheckType", "product_detail_cost_check_type", res.data.costCheckType===null?null:res.data.costCheckType, "成本核算");
                admin.getDictList("checkStatus", "sys_yes_no", res.data.checkStatus===null?null:res.data.checkStatus, "是否质检");
                admin.getDictList("boxStatus", "sys_yes_no",res.data.boxStatus===null?null:res.data.boxStatus, "是否料箱管理");

                projectTableData = res.data.processProjectData;
                testItemData = res.data.testItemData;
                if (res.data.drawInfo) {
                    tzId = res.data.drawInfo.id;
                    tzVersion = res.data.drawInfo.versionName;
                }
                let name = res.data.cusProductFigure;
                let imagUrl = "";
                //初始化文件
                $('#addHeaderPdf').html('<div class="attachments">' +
                    '<div><img name="attachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' + name + '</span></div></div>');
                $('#attachmentPdf').val(res.data.attachmentPdfUrl);
                imagUrl = res.data.attachmentPdfUrl;
                $('#fileName').text(res.data.cusProductFigure);
                $(".attachments").click(function () {
                    let json = {};
                    json.attachment = imagUrl;
                    showViewAttachmentModel(json);
                })
                if (imagUrl && imagUrl.indexOf(".pdf") !== -1) {
                    PDFObject.embed(imagUrl, "#TuZhiPdf");
                } else {
                    let attachmentHtml = "";
                    imagUrl.split(",").forEach((attachmentUrl, index) => {
                        attachmentHtml += '<div><div><img style="max-width:620px" src="' + attachmentUrl + '"></div></div>';
                    });
                    $('#TuZhiPdf').html(attachmentHtml);
                }

                $("#TuZhiPdf").click(function () {
                    let json = {};
                    json.attachment = imagUrl;
                    json.remark = res.data.drawInfo.remark;
                    json.taskId = res.data.id;
                    json.drawId = res.data.drawInfo.id;
                    showViewAttachmentModel2(json);
                })

                //是否关联图纸的点击
                form.on('select(isRelevance)', function (data) {
                    console.log("data",data);
                    if (data.value === '1') {
                        $("#moldUploadInfo").show();
                    } else {
                        $("#moldUploadInfo").hide();
                    }
                })

                //默认隐藏
                if (res.data.drawInfo) {
                    let drawInfos = res.data.drawInfo;
                    if (res.data.drawInfo.isRelevance === "1") {
                        $("#moldUploadInfo").show();
                        $('#addMoldHeaderPdf').html('<div class="moldAttachments">' +
                            '<div><img name="moldAttachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' + res.data.drawInfo.moldDrawInfo.name + '</span></div></div>');
                        $('#attachmentMoldPdf').val(res.data.drawInfo.moldDrawInfo.fileUrl);
                        $('#moldRemark').val(res.data.drawInfo.moldDrawInfo.remark);
                        moldName = res.data.drawInfo.moldDrawInfo.name;
                        $(".moldAttachments").click(function () {
                            let json = {};
                            json.attachment = res.data.drawInfo.moldDrawInfo.fileUrl;
                            showViewAttachmentModel(json);
                        })
                    } else {
                        $("#moldUploadInfo").hide();
                    }
                    $("#isRelevance").val(res.data.drawInfo.isRelevance);

                /**
                 *  初始化sku主材
                 */
                    admin.initDataSelectAndMap("enterprise/product/admin/productDetailWMS/list", {categoryType: "production",limit:9999}, "mainSkuId",drawInfos.mainSkuId==null?null:drawInfos.mainSkuId , "SKU物料", "codeAndName", null, null);

                } else {
                    $("#moldUploadInfo").hide();
                }

                //初始化工序productProjectTable
                table.render({
                    elem: '#productProjectTable',
                    data: projectTableData,
                    id: 'productProjectTable',
                    limit: 9999,
                    cols: [[
                        {type: 'numbers', title: '序号'}
                        , {
                            field: 'name',
                            edit: 'text',
                            align: 'center',
                            title: '工序名称',
                            width: '10%',
                        }
                        , {field: 'proCode', edit: 'text', sort: true, title: '产品工序编码'}
                        , {field: 'sorted', edit: 'text', sort: true, title: '工序排序'}
                        , {field: 'content', edit: 'text', sort: true, title: '工序工艺要求'}
                        , {field: 'qualityParam', edit: 'text', sort: true, title: '工序质检要求'}
                        // , {field: 'skuGetCode', edit: 'text', sort: true, title: '领料料号'}
                        , {align: 'center', toolbar: '#productProcessTableBar', title: '操作', width: 100}
                    ]]
                });

                //如果为空查询关联的
                if (Array.isArray(testItemData) && testItemData.length === 0) {
                    admin.req('enterprise/product/admin/productItem/listByType', {productType:res.data.productTypeId}, function (res) {
                        if (res.code == 200) {
                            testItemData = res.data;
                        }
                        //初始化质检标准表
                        table.render({
                            elem: '#testItemTable',
                            data: testItemData,
                            id: 'testItemTable',
                            limit: 9999,
                            cols: [[
                                {type: 'numbers', title: '序号'}
                                , {
                                    field: 'name',
                                    edit: 'text',
                                    align: 'center',
                                    title: '检查项'
                                }
                                , {field: 'standard', edit: 'text', sort: true, title: '标准'}
                                , {field: 'utensil', edit: 'text', sort: true, title: '器具'}
                                , {field: 'way', edit: 'text', sort: true, title: '方法'}
                                , {field: 'importance', edit: 'text', sort: true, title: '重要程度'}
                                , {field: 'detectionRate', edit: 'text', sort: true, title: '检测率'}
                                , {align: 'center', toolbar: '#testItemTableBar', title: '操作', width: 100}
                            ]]
                        });
                    })
                }else{
                    //初始化质检标准表
                    table.render({
                        elem: '#testItemTable',
                        data: testItemData,
                        id: 'testItemTable',
                        limit: 9999,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            , {
                                field: 'name',
                                edit: 'text',
                                align: 'center',
                                title: '检查项'
                            }
                            , {field: 'standard', edit: 'text', sort: true, title: '标准'}
                            , {field: 'utensil', edit: 'text', sort: true, title: '器具'}
                            , {field: 'way', edit: 'text', sort: true, title: '方法'}
                            , {field: 'importance', edit: 'text', sort: true, title: '重要程度'}
                            , {field: 'detectionRate', edit: 'text', sort: true, title: '检测率'}
                            , {align: 'center', toolbar: '#testItemTableBar', title: '操作', width: 100}
                        ]]
                    });
                }

                //初始化 bom
                // 渲染表格
               let bomTree = tableTree.render({
                    id: '#bomTreeTableInfo',
                    elem: '#bomTreeTableInfo',
                    url: config.base + 'enterprise/res/admin/planning/proBom/bomTree/' + data.productDetailId,
                    page: false,
                    cellMinWidth: 100,
                    where: {limit: 9999},
                    cols: [[
                        {type: 'numbers', title: '序号', fixed: 'left'}
                        , {field: 'productName', sort: true, title: 'BOM产品名称'}
                        , {field: 'productCode', sort: true, title: '产品编号'}
                        , {field: 'productSpec', sort: true, title: '规格码'}
                        , {field: 'productTypeName', sort: true, title: '物资类别'}
                        , {field: 'assemblyNatureName', sort: true, title: '产品性质'}
                        , {field: 'inSkuUnitName', sort: true, title: '计量单位', width: 90}
                        , {field: 'inUnitCount', edit: 'text', sort: true, title: '需求数', width: 80}
                        , {field: 'remark', edit: 'text', sort: true, title: '描述'}
                        , {align: 'center', toolbar: '#bomProductPlanningTableBar', title: '操作', width: 240}
                    ]]
                }, {keyPid: "parentId", title: "productName"})
                form.render(); //菜单渲染 把内容加载进去

                //addBomSKu 添加子件物料的点击事件
                $("#addBomSKu").click(function (){
                    showSkuListInfo(data.productDetailId, data.skuCode,"1","");
                })

                // 工具条点击事件
                table.on('tool(bomTreeTableInfo)', function (obj) {
                    if (obj.event === 'addDownInfo') { //添加下级
                        //addDownInfo
                        showSkuListInfo(obj.data.id, obj.data.productCode,"2",obj.data.bomId);
                    }else if(obj.event === 'removeInfo'){ //移除
                        //removeInfo
                        removeSkuInfo(obj.data.bomId);
                    }
                });

                // 显示编辑弹窗
                function showSkuListInfo(productDetailId,skuCode,flag,bomId) {
                    let title = "客户产品规划管理";
                    let parentInfo = {};
                    parentInfo.productDetailId = productDetailId;
                    parentInfo.flag = flag;
                    parentInfo.skuCode = skuCode;
                    parentInfo.bomId = bomId;
                    admin.putTempData("bomSkuData", parentInfo);
                    admin.formOpen({
                        type: 1,
                        area: ['1480px', '90%'],
                        style: 'font-size: 20px;',
                        title: title,
                        path: 'components/res/planning/skuDetailResearchForm.html',
                        end: function () {
                            bomTree.reload({}, 'data');
                        }
                    });
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        });

        // 查看附件
        function showViewAttachmentModel2(data) {
            admin.open({
                type: 1,
                area: ['65%', '900px'],
                offset: '35px',
                title: '修改图纸',
                content: $('#viewAttachmentHtml2').html(),
                success: function () {
                    console.log("data=>",data);
                    if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
                        PDFObject.embed(data.attachment, "#example1");
                    } else {
                        let attachmentHtml = "";
                        data.attachment.split(",").forEach((attachmentUrl, index) => {
                            attachmentHtml += '<div><img style="max-width: 900px;max-height: 650px" src="' + attachmentUrl + '"></div>';
                        });
                        $('#example1').html(attachmentHtml);
                    }

                    //赋值remark
                    $('#processDescription').val(data.remark);

                    $('#productPicBtn').click(uploadImg('productPicBtn', 'example1', 'productPic'));

                    //保存的点击事件
                    form.on('submit( )', function (d) {
                        layer.load(2);
                        let params = {};
                        params.type = "2";
                        params.taskId = data.taskId;
                        params.remark =  $('#processDescription').val();
                        params.drawId = data.drawId;
                        params.fileUrl = $('#productPic').val();
                        params.name = newFileName;
                        //新增修改
                        admin.req('enterprise/res/admin/proDraw/updateDrawVersion', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                    });
                }
            })
        }

        let newFileName = "";

        // 上传文件
        /*
        * _elem: 上传按钮的名字,
        * pathId: 显示图片容器的名字,
        * inputId: 提交文件路径元素的名字,
        * isMultiple: 是不是多文件(单文件为空或者false,多文件true)
        *
        * */
        function uploadImg(_elem, pathId, inputId, isMultiple) {
            upload.render({
                elem: '#' + _elem
                , url: config.upload_url + 'uploadFile'
                , data: {}
                , accept: 'file'
                , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                , headers: {Authorization: 'Bearer ' + config.getToken()}
                , done: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg || "上传成功", {icon: 1});
                        let imagUrl = res.data.url;
                        newFileName = res.data.fileName;
                        if (isMultiple == true) {
                            $('#' + pathId).append('<img style="max-width: 900px;max-height: 650px" src="' + imagUrl + '"/>');
                            $('#' + inputId).val(imagUrl);
                        } else {
                            $('#' + pathId).html('<img style="max-width: 900px;max-height: 650px" src="' + imagUrl + '"/>');
                            $('#' + inputId).val(imagUrl);
                        }
                    } else if (res.code == 502) {
                        return location.replace('admin/login.html');
                    } else {
                        layer.msg(res.msg || "上传失败", {icon: 2});
                    }
                }
            });
        }



        //移除下级物料
        function removeSkuInfo(bomId){
            layer.confirm('确认要移除该子件吗？',{
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                //添加bom子件信息
                admin.req('enterprise/res/admin/planning/proBom/deleteBom/'+bomId,{}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            })
        }


        //下拉款的选择事件
        form.on('select(processLineTemplate)', function (data) {
            let id = data.value;
            if (id) {
                //查询工艺模板关联的工序
                admin.req('enterprise/product/admin/processLineTemplate/listAndProcess?type=1&templateId=' + id + '&page=1&limit=999', {}, function (res) {
                    if (res.code == 200) {
                        let processes = res.data;
                        processes.forEach(value => {
                            let rowInfo = {
                                name: value.nodeCodes,
                                proCode: "",
                                sorted: "",
                                content: "",
                                qualityParam: "",
                                // skuGetCode: ""
                            }
                            projectTableData.push(rowInfo);
                        });
                        table.reload('productProjectTable', {
                            data: projectTableData
                        });
                    }
                })
            }
        });

        //如果产品属性是加工件 就只有工序
        if (data.assemblyNatureId === "3") {
            bom_tab.style.display = 'none';
            bom_content.style.display = 'none';
            //加载工序模板
            admin.initDataSelect("enterprise/product/admin/processLineTemplate/list?page=1&limit=999", null, "processLineTemplate", null, "模板", "name", "id");
        } else {
            gx_tab.style.display = 'none';
            gx_content.style.display = 'none';
        }

        //移除某一行
        table.on('tool(productProjectTable)', function (obj) {
            if (obj.event === 'delProcess') { //删除
                obj.del(); // 删除当前行
                // 获取表格数据
                projectTableData = table.cache['productProjectTable'];
                projectTableData =  projectTableData.filter(item => Object.keys(item).length > 0);
                table.reload('productProjectTable', {
                    data: projectTableData
                });
            }
        });

        //移除某一行
        table.on('tool(testItemTable)', function (obj) {
            if (obj.event === 'delTestItem') { //删除
                obj.del(); // 删除当前行
                // 获取表格数据
                testItemData = table.cache['testItemTable'];
                testItemData =  testItemData.filter(item => Object.keys(item).length > 0);
                table.reload('testItemTable', {
                    data: data
                });
            }
        });

        // 上传附件-pdf
        upload.render({
            elem: '#fileUploadMoldBtn'
            , url: config.upload_url + 'uploadFile'
            , data: {}
            , accept: 'file'
            , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
            , headers: {Authorization: 'Bearer ' + config.getToken()}
            , done: function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg || "上传成功", {icon: 1});
                    let imagUrl = res.data.url;
                    moldName = res.data.fileName;
                    fileName = res.data.fileName;
                    $('#addMoldHeaderPdf').html('<div class="moldAttachments">' +
                        '<div><img name="moldAttachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' + moldName + '</span></div></div>');
                    $('#attachmentMoldPdf').val(imagUrl);

                    $(".moldAttachments").click(function () {
                        let json = {};
                        json.attachment = imagUrl;
                        showViewAttachmentModel(json);
                    })
                } else if (res.code == 502) {
                    return location.replace('admin/login.html');
                } else {
                    layer.msg(res.msg || "上传失败", {icon: 2});
                }
            }
        });

        /**
         * 添加
         */
        $("#addProcessOne").click(function () {
            let newRow = {
                name: "",
                proCode: "",
                sorted: "",
                content: "",
                qualityParam: "",
                // skuGetCode: ""
            };
            projectTableData.push(newRow);
            table.reload('productProjectTable', {
                data: projectTableData
            })
        })

        //addTestItem
        $("#addTestItem").click(function () {
            let newRow = {
                name: "",
                standard: "",
                utensil: "",
                way: "",
                importance: "",
                detectionRate: ""
            };
            testItemData.push(newRow);
            table.reload('testItemTable', {
                data: testItemData
            })
        })

        //保存检测项点击
        $("#saveTestItem").click(function () {
            let param = {};
            param.skuCode = $("#inSkuCode").val();
            param.productDetailId = $("#productDetailId").val();
            let listInfo = table.cache['testItemTable'];
            let testItemData = [];
            if (Array.isArray(listInfo) && listInfo.length !== 0){
                listInfo.forEach(item=>{
                    let info = {};
                    info.name = item.name;
                    info.standard = item.standard;
                    info.utensil = item.utensil;
                    info.way = item.way;
                    info.importance = item.importance;
                    info.detectionRate = item.detectionRate;
                    testItemData.push(info);
                })
            }
            param.testItemData = testItemData;
            admin.req('enterprise/res/admin/planning/proTask/addSkuQuality',JSON.stringify(param), function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST',true,'application/json; charset=utf-8');
        })

        //保存按钮的点击事件
        $("#saveProjectUp").click(function () {
            let param = {};
            param.templateId = $("#processLineTemplate").val();
            param.skuCode = $("#inSkuCode").val();
            param.tzId = tzId;
            param.tzVersion = tzVersion;
            param.isRelevance = $("#isRelevance").val();
            param.mainSkuId = $("#mainSkuId").val();
            param.productDetailId = $("#productDetailId").val();
            param.fileUrl = $('#attachmentMoldPdf').val();
            param.moldName = moldName;
            param.moldRemark = $('#moldRemark').val();
            let listInfo = table.cache['productProjectTable'];
            let processProjectData = [];
            if (Array.isArray(listInfo) && listInfo.length !== 0) {
                listInfo.forEach(item => {
                    let info = {};
                    info.name = item.name;
                    info.content = item.content;
                    info.proCode = item.proCode;
                    info.qualityParam = item.qualityParam;
                    info.sorted = item.sorted;
                    processProjectData.push(info);
                })
            }
            param.projectTableData = processProjectData;
            admin.req('enterprise/res/admin/planning/proTask/addSkuProcess', JSON.stringify(param), function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST', true, 'application/json; charset=utf-8');
        })

        //提交校对的点击事件
        $("#submitProofread").click(function (){
            let id = data.id;
            admin.req('enterprise/res/admin/planning/proTask/submitProofread/'+id,{}, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productTaskTableInfo', {});
                    layer.closeAll('page');
                } else {
                    layer.msg(res.msg, {icon: 2});
                    layer.closeAll('page');
                }
            }, 'POST');
        });

        // 查看附件
        function showViewAttachmentModel(data) {
            admin.open({
                type: 1,
                area: ['60%', '80%'],
                offset: '65px',
                title: '查看附件',
                content: $('#viewAttachmentHtml').html(),
                success: function () {
                    if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
                        PDFObject.embed(data.attachment, "#example1");
                    } else {
                        var attachmentHtml = "";
                        data.attachment.split(",").forEach((attachmentUrl, index) => {
                            attachmentHtml += '<div><div><img src="' + attachmentUrl + '"></div></div>';
                        });
                        $('#example1').html(attachmentHtml);
                    }
                }
            })
        }

    })

</script>